package service;

import com.alibaba.fastjson.JSONObject;
import edge.DeviceGrpc;
import edge.EdgeMessage;
import io.grpc.stub.StreamObserver;

/**
 * @ClassName DeviceGrpcImpl
 * @Description 实现类
 * @Author wang.shouyan
 * @Date 2023/6/7 21:15
 **/
public class DeviceGrpcImpl extends DeviceGrpc.DeviceImplBase {

    @Override
    public void deviceService(EdgeMessage.RequestData request, StreamObserver<EdgeMessage.Result> responseObserver) {
        String type = request.getType();
        if("3.1".equals(type)){ //属性设置
            System.out.println("propertySet request: " + JSONObject.toJSONString(request));

            EdgeMessage.Result.Builder resultBuilder = EdgeMessage.Result.newBuilder();
            resultBuilder.setCode("0");
            resultBuilder.setMsg("成功");
            resultBuilder.setType("3.2");

            //onNext()方法向客户端返回结果
            responseObserver.onNext(resultBuilder.build());
            //告诉客户端这次调用已经完成
            responseObserver.onCompleted();
        } else if("4.1".equals(type)){ //服务调用
            System.out.println("functionCall request: " + JSONObject.toJSONString(request));
            EdgeMessage.Result.Builder resultBuilder = EdgeMessage.Result.newBuilder();
            resultBuilder.setCode("0");
            resultBuilder.setMsg("成功");
            resultBuilder.setType("4.2");

            //onNext()方法向客户端返回结果
            responseObserver.onNext(resultBuilder.build());
            //告诉客户端这次调用已经完成
            responseObserver.onCompleted();
        }
    }
}
